<template>
	<view class="content">
		<view class="top-block">
			<view class="classify-tab-block">
				<view class="tab-content" style="flex-grow: 1;">
					<view :class="tabActive==0?'active':''" class="tab-item" @click="tabActive=0">全部</view>
					<view @click="tabActive=item.id" :class="tabActive==item.id?'active':''" class="tab-item"
						v-for="(item,index) in tabList" :key="item.id">{{item.name}}</view>
				</view>
			</view>
		</view>
		<view class="info-block">
			<view class="input-block">
				<view class="iconfont icon-sousuo" @click="getList(true)"></view>
				<input placeholder="请输入搜索关键字..." v-model="keyWords" @confirm="search"
					placeholder-style="color:#157DFA;font-size:30rpx;" />
			</view>
			<template>
				<view class="list-block" v-if="list.length>0">
					<view class="info-list" v-for="(item,index) in list" :key="item.id"
						@click="navigate('/page_other/shopping/detail?id='+item.id)">
						<view class="info-item">
							<image :src="item.coverImage" class="img" mode="aspectFill"></image>
							<view class="describe-item">
								<view class="name">{{item.name}}</view>
								<view class="describe">{{item.description}}</view>
								<view class="wage">￥<text class="txt">{{item.integral}}</text> 积分</view>
							</view>
						</view>
					</view>
				</view>
			</template>
			<view v-if="list.length>0&&!listConf.more" style="text-align: center;margin-top: 30rpx;">没有更多了哦</view>
			<view v-else style="text-align: center;margin-top: 30rpx;">暂无数据</view>
		</view>
	</view>
</template>

<script>
	import {
		getShoppingListApi,
		getTypeListApi
	} from '@/api/shopping.js'
	import {
		isMore
	} from '@/util/util.js'
	import {
		getStorage
	} from '@/util/auth.js';
	export default {
		data() {
			return {
				keyWords: '',
				tabActive: 0, //顶部tab
				tabList: [],
				listConf: {
					page: 1,
					limit: 10,
					more: true,
				},
				list: []
			}
		},
		watch: {
			tabActive: {
				handler() {
					this.getList(true)
					this.keyWords = ''
				}
			}
		},
		methods: {
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			search() {
				this.getList(true)
			},
			getTabList() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getTypeListApi({}).then(data => {
					uni.hideLoading()
					this.tabList = data
				}).catch(() => {
					uni.hideLoading()
				})
			},
			getList(refresh = false) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getShoppingListApi({
					page: this.listConf.page,
					limit: this.listConf.limit,
					type: this.tabActive,
					title: this.keyWords,
					villageId: getStorage('villageId')
				}).then(data => {
					uni.hideLoading()
					let list = data.list || [];
					this.count = data.count
					if (refresh) {
						this.list = list;

					} else {
						this.list = [...this.list, ...list]
					}
					this.listConf.more = isMore(this.listConf.page, this.listConf.limit, data.count);
				}).catch(() => {
					uni.hideLoading()
				})
			}

		},
		onReachBottom() {
			if (this.listConf.more) {
				this.listConf.page++;
				this.getList()
			}
		},
		onLoad() {
			this.getList(true)
			this.getTabList()
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.top-block {
			width: 100%;
			padding: 30rpx;
			display: flex;
			flex-direction: column;
			background: #157DFA;
			height: 200rpx;

			.classify-tab-block {
				width: 100%;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
				position: relative;
				margin-top: 20rpx;

				.tab-content {
					display: flex;
					flex-direction: row;
					//justify-content: space-around;
					overflow-x: scroll;

					.tab-item {
						padding: 10rpx 15rpx;
						font-weight: bold;
						font-size: 30rpx;
						margin-right: 20rpx;
						flex: none;
						color: #fff;
					}

					.tab-item.active {
						border-bottom: 3px solid #fff;
						font-size: 32rpx;
					}
				}

				.iconfont {
					font-size: 32rpx;
					font-weight: bold;
					color: #fff;
				}
			}
		}

		.info-block {
			display: flex;
			flex-direction: column;
			background: #F5F9FC;
			border-radius: 40rpx;
			margin-top: -50rpx;
			height: auto;
			position: relative;
			padding: 30rpx;
			min-height: 70vh;

			.input-block {
				//width: calc(100% - 60rpx);
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				background: #fff;
				border-radius: 40rpx;
				padding: 0 10rpx;
				height: 80rpx;
				background: #CADFFA;

				.iconfont {
					flex-grow: 0;
					color: #157DFA;
					padding-left: 30rpx;
					font-size: 36rpx;
				}

				input {
					flex-grow: 1;
					font-size: 30rpx;
					color: #157DFA;
					margin: 0 10rpx;
				}
			}

			.list-block {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				margin-bottom: 30rpx;


				.info-list {
					background: #fff;
					margin-top: 20rpx;
					border-radius: 20rpx;
					width: calc((100% - 20rpx) / 2);
					margin-right: 20rpx;

					.info-item {
						width: 100%;
						display: flex;
						flex-direction: column;

						.img {
							width: 100%;
							height: 360rpx;
							border-radius: 20rpx 20rpx 0 0;
						}

						.describe-item {
							padding: 30rpx;

							.name {
								font-size: 32rpx;
								font-weight: bold;
								margin-bottom: 15rpx;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}

							.describe {
								color: #828282;
								font-size: 28rpx;
								margin-bottom: 15rpx;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}

							.wage {
								color: #157DFA;
								font-weight: bold;
								font-size: 30rpx;

								.txt {
									font-size: 38rpx;
									margin-right: 10rpx;
								}
							}
						}
					}
				}

				.info-list:nth-child(2n) {
					margin-right: 0;
				}
			}
		}
	}
</style>